/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */
package com.ecarx.systemui.plugin.ui.dialog;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ecarx.systemui.plugin.PluginContext;
import com.ecarx.systemui.plugin.R;
import com.ecarx.systemui.plugin.controller.GPSSController;
import com.ecarx.systemui.plugin.ui.dialog.base.AutoCloseDialog;
import com.ecarx.systemui.plugin.ui.dialog.base.DialogManager;
import com.ecarx.systemui.plugin.utils.LogUtils;


public class GPSDialog extends AutoCloseDialog {
    private static final String TAG = "GPSDialogTag";
    private TextView gpsText;
    private final GPSSController gpssController;

    public GPSDialog(Context context) {
        super(context, R.style.CommonDialog);
        gpssController = GPSSController.getInstance(PluginContext.getInstance().getPluginContext());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCancelable(true);
        setCanceledOnTouchOutside(true);
        int type = DialogManager.TYPE_VOLUME_OVERLAY;
        Window window = getWindow();
        if (window == null) {
            LogUtils.e(TAG, "getWindow IS NULL");
            return;
        }
        window.requestFeature(Window.FEATURE_NO_TITLE);
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND
                | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR);
        window.addFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
        window.setType(type);
        window.setWindowAnimations(com.android.internal.R.style.Animation_Toast);
        final WindowManager.LayoutParams lp = window.getAttributes();
        lp.format = PixelFormat.TRANSLUCENT;
        lp.gravity = Gravity.START | Gravity.TOP;
        lp.x =1353;
        lp.y = 114;
        lp.privateFlags |= WindowManager.LayoutParams.SYSTEM_FLAG_SHOW_FOR_ALL_USERS;
        window.setAttributes(lp);
        setContentView(R.layout.dialog_gps);
        gpsText = findViewById(R.id.gps);
    }

    private void updateText() {
        if (gpssController == null) {
            LogUtils.d(TAG, "updateText: gpsController is null ");
            return;
        }
        int currentGpsStatus = gpssController.getCurrentGpsStatus();
        LogUtils.d(TAG, "updateText: currentGpsStatus = " + currentGpsStatus);
        gpsText.setVisibility(View.VISIBLE);
        switch (currentGpsStatus) {
            case GPSSController.GPS_STATUS_NO:
                gpsText.setText(R.string.str_gps_nosignal);
                break;
            case GPSSController.GPS_STATUS_WEAK:
                gpsText.setText(R.string.str_gps_weaksignal);
                break;
            default:
                dismiss();
                break;

        }

    }

    @Override
    protected void onStart() {
        super.onStart();
        startTimingClose();
    }

    @Override
    protected void onStop() {
        super.onStop();
        cancelTimingClose();
    }

    @Override
    public void show() {
        super.show();
        updateText();
    }

    @Override
    public void dismiss() {
        super.dismiss();
        LogUtils.d(TAG, "dismiss");
    }


}
